package com.example.controller;

import com.example.pojo.CommandParams;
import com.example.pojo.Result;
import com.example.service.CommandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class CommandController {

    @Autowired
    private CommandService commandService;

    /**
     * 命令下发
     */
    @PostMapping("/command")
    public Result sendCommand(@RequestHeader(value = "token", required = false) String token,
                              @RequestBody CommandParams commandParams)
            throws Exception {
        log.info("命令下发: {}", commandParams);

        if (token == null || token.isEmpty()) {
            return Result.error("404, 请求头不合法");
        }
        commandService.sendCommand(token, commandParams);

        return Result.success();
    }

}
